package ve.usb.coord.colision;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.coord.colision.hibernate.*;


/**
 * Manejo de elementos de la entidad Coordinacion.<br>
 */
public class AccionesCoordinacion extends CohesionAction {
    
/**
     * Acción llamada por Struts para ejecutar la acción CoordinacionBuscarI.
     * Prepara la vista de selección de criterios de búsqueda para una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward CoordinacionBuscarI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "coordinacionBuscar";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            HttpSession sess = request.getSession();
            {FCoordinacionBuscar fFCoordinacionBuscar = (FCoordinacionBuscar)sess.getAttribute("FCoordinacionBuscar");
             if (fFCoordinacionBuscar==null) {
                 fFCoordinacionBuscar = new FCoordinacionBuscar();
                 sess.setAttribute("FCoordinacionBuscar", fFCoordinacionBuscar);
             } else {
                 fFCoordinacionBuscar.reset(mapping, request);
             }
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción CoordinacionCrearI.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward CoordinacionCrearI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "coordinacionCrear";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            HttpSession sess = request.getSession();
            {FCoordinacion fFCoordinacion = (FCoordinacion)sess.getAttribute("FCoordinacion");
             if (fFCoordinacion==null) {
                 fFCoordinacion = new FCoordinacion();
                 sess.setAttribute("FCoordinacion", fFCoordinacion);
             } else {
                 fFCoordinacion.reset(mapping, request);
             }

            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción CoordinacionBuscar.
     * Captura los criterios de búsqueda y los fija en la sesión.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward CoordinacionBuscar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "CoordinacionListarI";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FCoordinacionBuscar fFCoordinacionBuscar = (FCoordinacionBuscar)form;

            request.getSession().setAttribute("CoordinacionBuscar", " where nombre like '%" + fFCoordinacionBuscar.getNombre() + "%'");

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción CoordinacionListarI.
     * Selecciona las entidades a ser listadas de acuerdo a los parámetros recibidos.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward CoordinacionListarI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "coordinacionListar";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String crit = (String)request.getSession().getAttribute("CoordinacionBuscar");
            if (crit==null) crit = "";
            else request.getSession().removeAttribute("CoordinacionBuscar");
            java.util.List<Coordinacion> list = (java.util.List<Coordinacion>)s.createQuery("from Coordinacion"+crit).list();
            request.setAttribute("L_Coordinacion", list);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción CoordinacionI.
     * Prepara la vista de actualización de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward CoordinacionI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "coordinacion";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idCoordinacion");
            Long idCoordinacion = (id==null) ? 
                (Long)request.getSession().getAttribute("idCoordinacion") :
                new Long(id);
            
            HttpSession sess = request.getSession();
            FCoordinacion fFCoordinacion = (FCoordinacion)sess.getAttribute("FCoordinacion");
            if (fFCoordinacion==null) {
                fFCoordinacion = new FCoordinacion();
                sess.setAttribute("FCoordinacion", fFCoordinacion);
            } else {
                fFCoordinacion.reset(mapping, request);
            }
            Coordinacion coordinacion = (Coordinacion)s.load(Coordinacion.class, idCoordinacion);
            fFCoordinacion.setIdCoordinacion(coordinacion.getIdCoordinacion());
            fFCoordinacion.setNombre(coordinacion.getNombre());
            fFCoordinacion.setDireccion(coordinacion.getDireccion());
            fFCoordinacion.setTelefono(coordinacion.getTelefono());

            Hibernate.initialize(coordinacion.getUsuarios());
            request.setAttribute("L_Usuario", coordinacion.getUsuarios()); 
            Hibernate.initialize(coordinacion.getPlanesDeEstudio());
            request.setAttribute("L_PlanDeEstudios", coordinacion.getPlanesDeEstudio());

            request.getSession().setAttribute("idCoordinacion", idCoordinacion);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción CoordinacionAct.
     * Registro de actualizaciones de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward CoordinacionAct(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "coordinacion";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FCoordinacion fFCoordinacion = (FCoordinacion)form;

            Coordinacion coordinacion = (Coordinacion)s.load(Coordinacion.class, new Long(fFCoordinacion.getIdCoordinacion()));
            coordinacion.setNombre(fFCoordinacion.getNombre());
            coordinacion.setDireccion(fFCoordinacion.getDireccion());
            coordinacion.setTelefono(fFCoordinacion.getTelefono());


            Hibernate.initialize(coordinacion.getUsuarios());
            request.setAttribute("L_Usuario", coordinacion.getUsuarios());
            Hibernate.initialize(coordinacion.getPlanesDeEstudio());
            request.setAttribute("L_PlanDeEstudios", coordinacion.getPlanesDeEstudio());

            request.getSession().setAttribute("idCoordinacion", coordinacion.getIdCoordinacion());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción CoordinacionElim.
     * Elimina la entidad seleccionada.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward CoordinacionElim(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "CoordinacionListarI";
        final String SALIDA_1 = "coordinacion";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idCoordinacion");
            Long idCoordinacion = (id==null) ? 
                (Long)request.getSession().getAttribute("idCoordinacion") :
                new Long(id);
            Coordinacion coordinacion = (Coordinacion)s.load(Coordinacion.class, idCoordinacion);
            s.delete(coordinacion);
            try {
                s.flush();
            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
                request.setAttribute("msg", "No se pudo eliminar entidad.");
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción CoordinacionCrear.
     * Registro de la creación de una coordinacion.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward CoordinacionCrear(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_1 = "PlanDeEstudiosCoordinacionCrearI";

        String salida = SALIDA_1;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FCoordinacion fFCoordinacion = (FCoordinacion)form;

            Coordinacion coordinacion = new Coordinacion();
            coordinacion.setNombre(fFCoordinacion.getNombre());
            coordinacion.setDireccion(fFCoordinacion.getDireccion());
            coordinacion.setTelefono(fFCoordinacion.getTelefono());

            s.save(coordinacion);
            s.flush();
            fFCoordinacion.setIdCoordinacion(coordinacion.getIdCoordinacion());

            request.getSession().setAttribute("idCoordinacion", coordinacion.getIdCoordinacion());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

}